<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/11
 * Time: 16:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Cate extends Base
{
    /**
     * 一级分类列表
     * @return mixed|\think\response\Json
     */
    public function firstCate(){
        if(request()->isAjax()){
            $cate_status = input('cate_status'); // 状态
            $cate_name = input('cate_name'); // 名称
            $where['cate_level'] = 1;
            if(isset($cate_status) && $cate_status != ""){
                $where['cate_status'] = $cate_status;
            }
            if(isset($cate_name) && $cate_name != ''){
                $where['cate_name'] = ['like', '%' . $cate_name . '%'];
            }
            
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('cate')->where($where)->count();// 获取总条数
            $lists = Db::name('cate')
                ->where($where)
                ->field('cate_id, cate_name, cate_sort, cate_status')
                ->order('cate_sort asc')
                ->page($NowPage, $limits)
                ->select();
            foreach($lists as $k => $v){
                $goods_num = Db::table('up_goods')
                    ->where('goods_f_id', $v['cate_id'])
                    ->where('goods_del', 1)
                    ->count();
                $lists[$k]['goods_num'] = $goods_num;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('cate/firstcate');
    }
    
    /**
     * 添加一级菜单
     * @return mixed|\think\response\Json
     */
    public function addFirstCate(){
        if(Request::instance()->isPost()){
            $cate_name = trim(input('cate_name'));
            $cate_status = input('cate_status');
            // 是否有重名的分类
            $is_have = Db::table('up_cate')
                ->where(['cate_name' => $cate_name, 'cate_level' => 1])
                ->find();
            if($is_have){
                return json(['code' => 100, 'msg' => '已有该分类']);
            }
            
            // 排序
            $sort = 1;
            $is_empty = Db::table('up_cate')
                ->where('cate_level', 1)
                ->order('cate_sort desc')
                ->limit(1)
                ->find();
            if($is_empty){
                $sort = $is_empty['cate_sort'] + 1;
            }
            $insert = [
                'cate_name' => $cate_name,
                'cate_sort' => $sort,
                'cate_level' => 1,
                'cate_fid' => 0,
                'cate_status' => $cate_status,
                'cate_time' => date('Y-m-d H:i:s'),
                'cate_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_cate')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('cate/addfirstcate');
    }
    
    /**
     * 编辑一级分类
     * @return mixed|\think\response\Json
     */
    public function firstCateEdit(){
        $cate_id = input('cate_id');
        if(Request::instance()->isPost()){
            $cate_name = trim(input('cate_name'));
            $cate_status = input('cate_status');
            // 是否有重名的分类
            $is_have = Db::table('up_cate')
                ->where('cate_name', $cate_name)
                ->where('cate_level', 1)
                ->where('cate_id', 'NEQ', $cate_id)
                ->find();
            if($is_have){
                return json(['code' => 100, 'msg' => '已有该分类']);
            }
            
            $update = [
                'cate_name' => $cate_name,
                'cate_status' => $cate_status,
                'cate_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_cate')->where('cate_id', $cate_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $cate = Db::table('up_cate')->field('cate_name, cate_status')->where('cate_id', $cate_id)->find();
        return $this->fetch('cate/firstcateedit', ['cate' => $cate, 'cate_id' => $cate_id]);
    }
    
    /**
     * 查看下级
     * @return mixed|\think\response\Json
     */
    public function nextCate(){
        if(request()->isAjax()){
            $where['cate_fid'] = session('cate_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('cate')->where($where)->count();// 获取总条数
            $lists = Db::name('cate')
                ->where($where)
                ->field('cate_id, cate_name, cate_sort, cate_status')
                ->order('cate_sort asc')
                ->page($NowPage, $limits)
                ->select();
            foreach($lists as $k => $v){
                $goods_num = Db::table('up_goods')
                    ->where('goods_s_id', $v['cate_id'])
                    ->where('goods_del', 1)
                    ->count();
                $lists[$k]['goods_num'] = $goods_num;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        session('cate_id', input('cate_id'));
        return $this->fetch('cate/nextcate');
    }
    
    /**
     * 修改一级分类状态
     * @return \think\response\Json
     */
    public function firstCateChange(){
        $cate_id = input('id');
        $cate_status = Db::table('up_cate')->where('cate_id', $cate_id)->value('cate_status');
        if($cate_status == 1){
            $status = 2;
        }else{
            $status = 1;
        }
        $res = Db::table('up_cate')->where('cate_id', $cate_id)->setField('cate_status', $status);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除一级分类
     * @return \think\response\Json
     */
    public function firstCateDel(){
        $cate_id = input('id');
        $is_have = Db::table('up_goods')
            ->where('goods_f_id', $cate_id)
            ->where('goods_del', 1)
            ->find();
        if($is_have){
            return json(['code' => 100, 'msg' => '该分类下有商品，不能直接删除']);
        }
        $res = Db::table('up_cate')->delete($cate_id);
        if($res){
            Db::table('up_cate')->where('cate_fid', $cate_id)->delete();
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    //***********  如果排序需要覆盖，则使用一下两种方法 **************//
    /**
     * 判断是否有重复的排序---备用
     * @return \think\response\Json
     */
    public function affirmSort(){
        $param = input('');
        $same = Db::table('up_cate')
            ->where('cate_sort', $param['value'])
            ->where('cate_level', $param['level'])
            ->where('cate_id', 'NEQ', $param['cate_id'])
            ->find();
        if($same){
            return json(['code' => 200, 'msg' => $same]);
        }else{
            return json(['code' => 100, 'msg' => '']);
        }
    }
    
    /**
     * 如果有相同排序，并且选择覆盖---备用
     * @return \think\response\Json
     */
    public function changeSort(){
        $param = input('');
        Db::startTrans();
        try{
            // 修改的记录的排序
            Db::table('up_cate')->where('cate_id', $param['cate_id'])->setField('cate_sort', $param['value']);
            // 查出排序的最大值
            $max_sort = Db::table('up_cate')
                ->where('cate_level', $param['level'])
                ->max('cate_sort');
            $new_sort = $max_sort + 1; // 新的排序
            // 被覆盖记录的排序
            Db::table('up_cate')->where('cate_id', $param['same_id'])->setField('cate_sort', $new_sort);
            Db::commit();
            return json(['code' => 200, 'msg' => '修改成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }
    //***********  如果排序需要覆盖，则使用一下两种方法 **************//
    
    /**
     * 修改排序
     * @return \think\response\Json
     */
    public function firstCateSort(){
        $param = input('');
        $res = Db::table('up_cate')->where('cate_id', $param['id'])->setField('cate_sort', $param['value']);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 二级分类列表展示
     * @return mixed|\think\response\Json
     */
    public function secondCate(){
        if(request()->isAjax()){
            $cate_status = input('cate_status'); // 状态
            $cate_name = input('cate_name'); // 名称
            $where['cate_level'] = 2;
            if(isset($cate_status) && $cate_status != ""){
                $where['cate_status'] = $cate_status;
            }
            if(isset($cate_name) && $cate_name != ''){
                $where['cate_name'] = ['like', '%' . $cate_name . '%'];
            }
        
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('cate')->where($where)->count();// 获取总条数
            $lists = Db::name('cate')
                ->where($where)
                ->field('cate_id, cate_name, cate_sort, cate_status, cate_fid, cate_icon')
                ->order('cate_sort asc')
                ->page($NowPage, $limits)
                ->select();
            foreach($lists as $k => $v){
                // 商品数量
                $goods_num = Db::table('up_goods')
                    ->where('goods_s_id', $v['cate_id'])
                    ->where('goods_del', 1)
                    ->count();
                $lists[$k]['goods_num'] = $goods_num;
                // 上级
                $f_name = Db::table('up_cate')
                    ->where('cate_id', $v['cate_fid'])
                    ->value('cate_name');
                $lists[$k]['fname'] = $f_name;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('cate/secondcate');
    }
    
    /**
     * 添加二级分类
     * @return mixed|\think\response\Json
     */
    public function addSecondCate(){
        if(Request::instance()->isPost()){
            $cate_name = trim(input('cate_name'));
            $cate_status = input('cate_status');
            $cate_fid = input('cate_fid');
            $cate_icon = input('cate_icon');
            // 是否有重名的分类
            $is_have = Db::table('up_cate')
                ->where(['cate_name' => $cate_name, 'cate_level' => 2])
                ->find();
            if($is_have){
                return json(['code' => 100, 'msg' => '已有该分类']);
            }
    
            // 排序
            $sort = 1;
            $is_empty = Db::table('up_cate')
                ->where('cate_level', 2)
                ->order('cate_sort desc')
                ->limit(1)
                ->find();
            if($is_empty){
                $sort = $is_empty['cate_sort'] + 1;
            }
            $insert = [
                'cate_name' => $cate_name,
                'cate_sort' => $sort,
                'cate_level' => 2,
                'cate_fid' => $cate_fid,
                'cate_status' => $cate_status,
                'cate_icon' => $cate_icon,
                'cate_time' => date('Y-m-d H:i:s'),
                'cate_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_cate')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $cate = Db::table('up_cate')
            ->field('cate_id, cate_name')
            ->order('cate_sort asc')
            ->where('cate_status', 1)
            ->where('cate_level', 1)
            ->select();
        return $this->fetch('cate/addsecondcate', ['cate' => $cate]);
    }
    
    /**
     * 编辑二级分类
     * @return mixed|\think\response\Json
     */
    public function secondCateEdit(){
        $cate_id = input('cate_id');
        if(Request::instance()->isPost()){
            $cate_name = trim(input('cate_name'));
            $cate_status = input('cate_status');
            $cate_fid = input('cate_fid');
            $cate_icon = input('cate_icon');
            // 是否有重名的分类
            $is_have = Db::table('up_cate')
                ->where('cate_name', $cate_name)
                ->where('cate_level', 2)
                ->where('cate_id', 'NEQ', $cate_id)
                ->find();
            if($is_have){
                return json(['code' => 100, 'msg' => '已有该分类']);
            }
        
            $update = [
                'cate_name' => $cate_name,
                'cate_status' => $cate_status,
                'cate_fid' => $cate_fid,
                'cate_update_time' => date('Y-m-d H:i:s')
            ];
            if(!empty($cate_icon)){
                $update['cate_icon'] = $cate_icon;
            }
            $res = Db::table('up_cate')->where('cate_id', $cate_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        // 一级分类列表
        $cateList = Db::table('up_cate')
            ->field('cate_id, cate_name')
            ->order('cate_sort asc')
            ->where('cate_status', 1)
            ->where('cate_level', 1)
            ->select();
        // 二级分类详情
        $cate = Db::table('up_cate')
            ->field('cate_name, cate_status, cate_icon, cate_fid')
            ->where('cate_id', $cate_id)
            ->find();
        $this->assign([
            'cateList' => $cateList,
            'cate' => $cate,
            'cate_id' => $cate_id
        ]);
        return $this->fetch('cate/secondcateedit');
    }
    
    /**
     * 删除二级分类
     * @return \think\response\Json
     */
    public function secondCateDel(){
        $cate_id = input('id');
        $is_have = Db::table('up_goods')
            ->where('goods_s_id', $cate_id)
            ->where('goods_del', 1)
            ->find();
        if($is_have){
            return json(['code' => 100, 'msg' => '该分类下有商品，不能直接删除']);
        }
        $res = Db::table('up_cate')->delete($cate_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

}